#include <iostream>
#include <stack>

using namespace std;

typedef long long LL;

const int N = 1e6 + 10;

struct node{
	LL h;
	LL v;
} a[N];

LL n;
int ret1[N], ret2[N];
LL ret[N];

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i].h >> a[i].v;
	
	stack<int> st;
	for(int i = 1; i <= n; i++)
	{
		while(st.size() && a[st.top()].h <= a[i].h) st.pop();
		if(st.size()) ret1[i] = st.top();
		st.push(i);
	}
	while(st.size()) st.pop();
	for(int i = n; i >= 1; i--)
	{
		while(st.size() && a[st.top()].h <= a[i].h) st.pop();
		if(st.size()) ret2[i] = st.top();
		st.push(i);
	}
	
	for(int i = 1; i <= n; i++)
	{
		int x = ret1[i], y = ret2[i];
		ret[x] += a[i].v;
		ret[y] += a[i].v;
	}
	
	LL ans = 0;
	for(int i = 1; i <= n; i++)
	{
		ans = max(ans, ret[i]);
	}
	
	cout << ans << endl;
	
	return 0;
}
